/**
 * Created on May 11, 2007
 */
package jvs.vfs.tools;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import x.java.lang.System;

/**
 * @author qiangli
 * 
 */
public class Desktop extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private static final String SYS_NAME = "Java Virtual System (JVS=JVM+VFS)";

	private static final String ABOUT_TEXT = "Java Virtual System - a common platform for all Java applications.\n\n"
			+ "Written by Li, Qiang (C) Copyright 2007. Released under Apache License 2.0.\n\n"
			+ "This software uses work from Sun Microsystems, Apache Software Foundation, Day Software\n"
			+ "and their dependent libraries.";

	//
	public static JDesktopPane desktop = null;

	public Desktop() {
		super(SYS_NAME);

		// int inset = 100;
		int inset = 200;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height
				- inset * 2);
		desktop = new JDesktopPane();
		desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
		setContentPane(desktop);
		setJMenuBar(createMenuBar());
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}

	/**
	 * @return
	 */
	private JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();

		JMenu menu = new JMenu("System");
		menuBar.add(menu);

		JMenuItem mi = new JMenuItem("Shutdown");
		mi.setActionCommand("quit");
		mi.addActionListener(this);
		menu.add(mi);

		menu = new JMenu("Application");
		menuBar.add(menu);

		mi = new JMenuItem("VFS Explorer");
		mi.setActionCommand("explorer");
		mi.addActionListener(this);
		menu.add(mi);

		mi = new JMenuItem("Notepad");
		mi.setActionCommand("notepad");
		mi.addActionListener(this);
		menu.add(mi);

		menu = new JMenu("Service");
		menuBar.add(menu);

		mi = new JMenuItem("FTP Server");
		mi.setActionCommand("ftpd");
		mi.addActionListener(this);
		menu.add(mi);

		mi = new JMenuItem("Telnet Server");
		mi.setActionCommand("telnetd");
		mi.addActionListener(this);
		menu.add(mi);

		menu = new JMenu("Help");
		menuBar.add(menu);

		mi = new JMenuItem("About JVS");
		mi.setActionCommand("about");
		mi.addActionListener(this);
		menu.add(mi);

		return menuBar;
	}

	private static void showGUI() {
		JFrame.setDefaultLookAndFeelDecorated(true);
		Desktop frame = new Desktop();

		frame.setVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent ae) {
		if ("quit".equals(ae.getActionCommand())) {
			exit();
		} else if ("explorer".equals(ae.getActionCommand())) {
			try {
				Shell.exec("fe");
			} catch (Exception e) {
				showMessageDialog(e.getMessage(), "", JOptionPane.ERROR_MESSAGE);
			}

		} else if ("notepad".equals(ae.getActionCommand())) {
			try {
				Shell.exec("notepad");
			} catch (Exception e) {
				showMessageDialog(e.getMessage(), "", JOptionPane.ERROR_MESSAGE);
			}

		} else if ("about".equals(ae.getActionCommand())) {
			showMessageDialog(ABOUT_TEXT, "About JVS",
					JOptionPane.PLAIN_MESSAGE);
		}
	}

	private void showMessageDialog(String msg, String title, int type) {

		JOptionPane.showInternalMessageDialog(desktop, msg, title, type);

	}

	private void exit() {
		int op = JOptionPane.showConfirmDialog(desktop, "Are you sure?",
				"Shutdown", JOptionPane.YES_NO_OPTION);
		if (op == JOptionPane.OK_OPTION) {
			System.shutdown(0);
		}
	}

	public static void main(String[] args) {
		try {
			// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			UIManager.setLookAndFeel(UIManager
					.getCrossPlatformLookAndFeelClassName());
		} catch (Exception e) {
		}
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				showGUI();
			}
		});
	}
}

class BaseFrame extends JInternalFrame {
	private static final long serialVersionUID = 1L;

	private static int openFrameCount = 0;

	private static final int xOffset = 30, yOffset = 30;

	public BaseFrame(String title) {
		super(title, // title
				true, // resizable
				true, // closable
				true, // maximizable
				true);// iconifiable
		//
		Component np = buildUI();
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(np, BorderLayout.CENTER);
		setSize(500, 450);
		setLocation(xOffset * openFrameCount, yOffset * openFrameCount);
		openFrameCount++;
	}

	protected Component buildUI() {
		return null;
	}
}
